﻿using DG.Tweening;
using JetBrains.Annotations;
using System;
using System.Collections;
using System.ComponentModel;
using UnityEngine;
using UnityEngine.InputSystem.LowLevel;

public class IronHeadMonster : Actor,IDamageable
{
    #region AI 逻辑
    protected override void Awake()
    {
        base.Awake();
        _collider = GetComponent<Collider2D>();
    }
    protected override void Update()
    {
        UpdateAIAgent();
       
    }
    private void OnEnable()
    {
        _collider.enabled = true;
        AddEvent();
    }

    

    private void OnDisable()
    {
        _collider.enabled = false;
        RemoveEvent();
    }
    
    #endregion

    private void UpdateAIAgent()
    {
        if (_death)
        {
            Die();
            return;
        }
        if (_isTurn)
        {
            Turn();
            return;
        }


        if (HasThingFont)
        {
            Static();
        }
        else if (!OnGrounded)
        {
            //转向
            Diversion();
        }
        else
        {
            Patrol();
        }
    }

    private bool _turn=false;
    [SerializeField] private float _duration = 0.5f;
    private void Diversion()
    {  
        if (!_turn) 
        {
            _turn = true;
            Flip();
            SetVelocityX(_moveSpeed * _direction);
            StartCoroutine(TurnCoro());
        }
    }

    private IEnumerator TurnCoro()
    {
        yield return new WaitForSeconds(_duration);
        _turn = false;

    }
    


    //巡逻
    [SerializeField] protected float _moveSpeed;
    public void Patrol() 
    {
        PlayAnim("move");
        SetVelocityX(_moveSpeed*_direction);
    }

    //静止
    private void Static()
    {
        SetVelocityZero();
        //等待1秒
        Flip();
    }


    //翻倒
    private void AddEvent()
    {
        EventCenter.AddEventListener(GameEventType.ALL_ENEMIES_TURN, SetTurn);
    }

    

    private void RemoveEvent()
    {
        EventCenter.RemoveEventListener(GameEventType.ALL_ENEMIES_TURN, SetTurn);
    }
   

    private bool _isTurn=false;
    [SerializeField]private float _turnTime;
    [SerializeField] private float _resetTurnTime;
    private float _timer=0;
  
    public void SetTurn() 
    {
        _isTurn = !_isTurn;
        _timer = 0;
    }

    public void Turn() 
    {
        if (_isTurn) 
        {
            SetVelocityZero();
            transform.DORotate(new Vector3(0, 0, 180), _turnTime);
            _timer+=Time.deltaTime;
            if (_resetTurnTime - _timer <= 0) 
            {
                transform.DORotate(new Vector3(0, 0, 0), _turnTime);
                _isTurn = false;
                _timer = 0;
            }
        }
    }

 

    #region 死亡
    //
    private Collider2D _collider;   

    [Header("死亡参数")]
    [SerializeField] private float _gravityScale;
    [SerializeField] private Vector2 _angle = new Vector2(1, 1);
    [SerializeField] private float _force = 10;
    [SerializeField] private float _interval = 0.2f;

    public void InitCollider() 
    {
        _collider = GetComponent<Collider2D>();
    }

    private bool _death;
    
    private void Die()
    {
        PlayAnim("die");
        //开始死亡动画
    }
    public void TakeDamage(AttackDetail detail)
    {
        Debug.Log("击中怪物");
        _death = true;
        _collider.enabled = false;
        Die();
        StartCoroutine(DeathAnim(detail.Direction));
        Destroy(gameObject, 1f);
    }
    private IEnumerator DeathAnim(int direction)
    {
        transform.DORotate(new Vector3(0, 0, 180), 0.5f).SetLoops(1000);
        SetGravityScale(0);
        SetVelocity(_force,new Vector2(_angle.x*direction,_angle.y));
        yield return new WaitForSeconds(_interval);
        SetGravityScale(_gravityScale);
    }
    #endregion

    #region 碰撞检测
    private void OnCollisionEnter2D(Collision2D collision)
    {
        var damageable=collision.gameObject.GetComponent<IDamageable>();
        if(damageable!=null && collision.gameObject.CompareTag("Player")) 
        {
            AudioManager.Instance.PlaySound(SoundType.HitMonster);
            AttackDetail detail = new AttackDetail();
            detail.Direction = _direction;
            damageable.TakeDamage(detail);
        }
    }

    #endregion

    
}
